package com.xdcplus.web.service.wip.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingPrdFilterDTO;
import com.xdcplus.web.common.pojo.query.wip.WipMarkingPrdQuery;
import com.xdcplus.web.mapper.wip.WipMarkingPrdMapper;
import com.xdcplus.web.common.pojo.entity.wip.WipMarkingPrd;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingPrdDTO;
import com.xdcplus.web.common.pojo.vo.wip.WipMarkingPrdVO;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.wip.WipMarkingPrdService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * 打标单片/花篮表(WipMarkingPrd)表服务实现类
 *
 * @author Richard.Li
 * @since 2024-08-20 18:49:36
 */
@Slf4j
@Service("wipMarkingPrdService")
public class WipMarkingPrdServiceImpl extends BaseServiceImpl<WipMarkingPrdMapper, WipMarkingPrd, WipMarkingPrd, WipMarkingPrdVO> implements WipMarkingPrdService {

    @Autowired
    protected WipMarkingPrdMapper wipMarkingPrdMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingPrd(WipMarkingPrdDTO wipMarkingPrdDTO) {

        WipMarkingPrd wipMarkingPrd = new WipMarkingPrd();
        BeanUtil.copyProperties(wipMarkingPrdDTO, wipMarkingPrd);

        return this.save(wipMarkingPrd);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingPrd(WipMarkingPrdDTO wipMarkingPrdDTO) {

        WipMarkingPrd wipMarkingPrd = this.getById(wipMarkingPrdDTO.getId());
        if (ObjectUtil.isNull(wipMarkingPrd)) {
            log.error("updateWipMarkingPrd() The WipMarkingPrd does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingPrdDTO, wipMarkingPrd);

        return this.updateById(wipMarkingPrd);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingPrd> wipMarkingPrdList) {

        wipMarkingPrdList.forEach(wipMarkingPrd -> {
            WipMarkingPrd wipMarkingPrdParam = new WipMarkingPrd();
            wipMarkingPrdParam.setId(wipMarkingPrd.getId());
            if (ObjectUtil.isNotNull(wipMarkingPrd.getId())) {
                wipMarkingPrd.setId(wipMarkingPrd.getId());
                LambdaUpdateWrapper<WipMarkingPrd> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingPrd::getId, wipMarkingPrd.getId());
                update(wipMarkingPrd, lambdaUpdate);
            } else {
                save(wipMarkingPrd);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingPrdDTO> wipMarkingPrdDTOList) {

        List<WipMarkingPrd> wipMarkingPrdList = BeanUtils.copyProperties(wipMarkingPrdDTOList, WipMarkingPrd.class);
        return saveOrUpdateBatch(wipMarkingPrdList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingPrdLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingPrd wipMarkingPrd = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingPrd)) {
            log.error("deleteWipMarkingPrd() The WipMarkingPrd does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingPrdByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingPrd wipMarkingPrd = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingPrd)) {
				log.error("deleteWipMarkingPrd() The WipMarkingPrd does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingPrd> queryWipMarkingPrdList(WipMarkingPrdFilterDTO wipMarkingPrdFilterDTO) {
        WipMarkingPrdQuery wipMarkingPrdQuery = BeanUtil.copyProperties(wipMarkingPrdFilterDTO, WipMarkingPrdQuery.class);

        return wipMarkingPrdMapper.queryWipMarkingPrd(wipMarkingPrdQuery);
    }

    @Override
    public List<WipMarkingPrdVO> queryWipMarkingPrdVOList(WipMarkingPrdFilterDTO wipMarkingPrdFilterDTO) {
        return this.objectConversion(queryWipMarkingPrdList(wipMarkingPrdFilterDTO));
    }

    @Override
    public PageVO<WipMarkingPrdVO> queryWipMarkingPrd(WipMarkingPrdFilterDTO wipMarkingPrdFilterDTO) {
        PageVO<WipMarkingPrdVO> pageVO = new PageVO<>();

        if (wipMarkingPrdFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingPrdFilterDTO.getCurrentPage(), wipMarkingPrdFilterDTO.getPageSize(),
                    wipMarkingPrdFilterDTO.getOrderType(), wipMarkingPrdFilterDTO.getOrderField());
        }

        List<WipMarkingPrd> wipMarkingPrdList = queryWipMarkingPrdList(wipMarkingPrdFilterDTO);

        PageInfo<WipMarkingPrd> pageInfo = new PageInfo<>(wipMarkingPrdList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingPrdList));

        return pageVO;
    }

    @Override
    public WipMarkingPrdVO queryWipMarkingPrdById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
